package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.model.Option;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.SysDegree;
import com.youlai.system.model.entity.SysScore;
import com.youlai.system.model.query.DegreePageQuery;
import com.youlai.system.model.vo.DegreePageVO;
import com.youlai.system.model.vo.DegreeScoreVO;
import com.youlai.system.security.util.SecurityUtils;
import com.youlai.system.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 初级定级控制器
 */
@Tag(name = "18.初级定级接口")
@RestController
@RequestMapping("/api/v1/degrees")
@RequiredArgsConstructor
public class SysDegreeController {

    private final SysDegreeService degreeService;

    private final SysScoreService scoreService;

    private final SysMeasureService measureService;

    private final SysLocationService locationService;

    private final SysQuestionService questionService;

    @Operation(summary = "初级定级分页列表")
    @GetMapping("/page")
    public PageResult<DegreePageVO> getScorePage(@ParameterObject DegreePageQuery queryParams) {
        if (SecurityUtils.isStudent()) {
            queryParams.setLoginUserId(SecurityUtils.getUserId());
        }
        IPage<DegreePageVO> result = degreeService.getDegreePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "某个培训司机某个故障初级用的成绩列表数据")
    @GetMapping("/{degreeId}/getPrimaryDegreeScoreList")
    public Result<List<DegreeScoreVO>> getPrimaryDegreeScoreList(@Parameter(description = "初级定级ID") @PathVariable Long degreeId) {
        SysDegree degree = degreeService.getById(degreeId);
        SysScore score = scoreService.getById(degree.getLastScoreId());
        List<SysScore> list = scoreService.getPrimaryDegreeScoreList(degree.getStudentId(), degree.getCourseId(), score.getRealTime());
        List<Option> measureList = measureService.listMeasureOptions();
        List<Option> locationList = locationService.listLocationOptions();
        List<Option> questionList = questionService.listQuestionOptions(null);
        Map<Long, String> measureMap = measureList.stream().collect(Collectors.toMap(it -> (Long) it.getValue(), Option::getLabel));
        Map<Long, String> locationMap = locationList.stream().collect(Collectors.toMap(it -> (Long) it.getValue(), Option::getLabel));
        Map<Long, String> questionMap = questionList.stream().collect(Collectors.toMap(it -> (Long) it.getValue(), Option::getLabel));
        List<DegreeScoreVO> degreeScoreVOList = list.stream().map( sysScore -> {
            DegreeScoreVO degreeScoreVO = new DegreeScoreVO();
            degreeScoreVO.setCheckDate(sysScore.getCheckDate());
            degreeScoreVO.setCheckTime(sysScore.getCheckTime());
            degreeScoreVO.setScore(sysScore.getScore());
            degreeScoreVO.setQuestionContent(sysScore.getQuestionContent());
            degreeScoreVO.setQuestionCount(sysScore.getQuestionCount());
            degreeScoreVO.setRemark(sysScore.getContent());
            String measureName = measureMap.get(sysScore.getMeasureId());
            degreeScoreVO.setMeasureName(measureName);
            String locationName = locationMap.get(sysScore.getLocationId());
            degreeScoreVO.setLocationName(locationName);
            String questionName = questionMap.get(sysScore.getQuestionId());
            degreeScoreVO.setQuestionName(questionName);
            return degreeScoreVO;
        }).collect(Collectors.toList());
        return Result.success(degreeScoreVOList);
    }
}
